package org.example;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Test12306 {
    private WebDriver webDriver;

    //登录网站，并将鼠标置于悬浮窗
    @BeforeEach
    public void SetUp(){
        webDriver = new ChromeDriver();
        webDriver.get("https://www.12306.cn/index");
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        //定位鼠标元素到悬浮窗
        WebElement FloatingBox = webDriver.findElement(By.cssSelector("#J-chepiao > a"));
        Actions actions = new Actions(webDriver);
        actions.moveToElement(FloatingBox).perform();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    }
    @ParameterizedTest
    @CsvSource({
            "北京北,上海,2024-10-20,12306_search_R002_001.png",
            "北京北,上海,2024-10-22,12306_search_R002_002.png",
            "北京北,上海,2024-10-26,12306_search_R001_001.png",
            "北京北,上海,2024-10-31,12306_search_R001_002.png",
            "北京北,上海,2024-10-32,12306_search_R002_003.png",
    })
    public void test_12306_R001_and_R002(String from, String to, String fromTime, String screenShot) throws InterruptedException {
        //点击单程
        webDriver.findElement(By.linkText("单程")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        //定位起始点
        WebElement fromField = webDriver.findElement(By.id("fromStationText"));
        fromField.click();
        fromField.sendKeys(from + Keys.ENTER);
        //定位目的地
        WebElement toField = webDriver.findElement(By.id("toStationText"));
        toField.click();
        toField.sendKeys(to + Keys.ENTER);
        //先清空时间，再添加
        WebElement fromTimeField = webDriver.findElement(By.id("train_date"));
        fromTimeField.clear();
        fromTimeField.sendKeys(fromTime);
        //点击查询按钮
        WebElement query = webDriver.findElement(By.id("query_ticket"));
        query.click();
        //截图
        Thread.sleep(2500);
        takeScreenshot(screenShot);
    }

    @ParameterizedTest
    @CsvSource({
            "北京北,上海,2024-11-01,2024-11-02, 12306_search_R004_001.png",
            "北京北,上海,2024-11-01,2024-11-30, 12306_search_R004_002.png",
            "北京北,上海,2024-11-02,2024-10-26, 12306_search_R005_001.png",
            "北京北,上海,2024-11-02,2024-11-31, 12306_search_R005_002.png",
            "北京北,上海,2024-11-02,2025-10-25, 12306_search_R005_003.png",
    })
    public void test_12306_R004_and_R005(String from, String to, String fromTime, String toTime, String screenShot) throws InterruptedException {
        //点击往返
        webDriver.findElement(By.linkText("往返")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        //定位起始点
        WebElement fromField = webDriver.findElement(By.id("fromStationText"));
        fromField.click();
        fromField.sendKeys(from + Keys.ENTER);
        //定位目的地
        WebElement toField = webDriver.findElement(By.id("toStationText"));
        toField.click();
        toField.sendKeys(to + Keys.ENTER);
        //起始日
        WebElement fromTimeField = webDriver.findElement(By.id("train_date"));
        fromTimeField.clear();
        fromTimeField.sendKeys(fromTime);
        //终止日
        WebElement backTimeField = webDriver.findElement(By.id("back_train_date"));
        backTimeField.clear();
        backTimeField.sendKeys(toTime);
        //点击查询按钮
        WebElement query = webDriver.findElement(By.id("query_ticket"));
        query.click();
        //截图
        Thread.sleep(2500);
        takeScreenshot(screenShot);
    }

    @ParameterizedTest
    @CsvSource({
            "哈尔滨,济南,2024-10-27, 12306_search_R007_001.png",
            "哈尔滨,西安,2024-10-27, 12306_search_R007_002.png",
            "哈尔滨,安吉,2024-10-27, 12306_search_R007_003.png",
    })
    public void test_12306_R007(String from, String to, String fromTime, String screenShot) throws InterruptedException {
        //点击中转换乘
        webDriver.findElement(By.linkText("中转换乘")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        //定位起始点
        WebElement fromField = webDriver.findElement(By.id("fromStationText"));
        fromField.click();
        fromField.sendKeys(from + Keys.ENTER);
        //定位目的地
        WebElement toField = webDriver.findElement(By.id("toStationText"));
        toField.click();
        toField.sendKeys(to + Keys.ENTER);
        //起始日
        WebElement fromTimeField = webDriver.findElement(By.id("train_start_date"));
        fromTimeField.clear();
        fromTimeField.sendKeys(fromTime);
        //点击查询按钮
        WebElement query = webDriver.findElement(By.id("_a_search_btn"));
        query.click();
        //截图
        Thread.sleep(2500);
        takeScreenshot(screenShot);
    }

    @ParameterizedTest
    @CsvSource({
            "哈尔滨,济南,2024-10-27, 厦门, 12306_search_R008_001.png",
            "哈尔滨,济南,2024-10-27, 福州, 12306_search_R008_002.png",
            "哈尔滨,济南,2024-10-27, 海南, 12306_search_R008_003.png",
    })
    public void test_12306_R008(String from, String to, String fromTime, String change, String screenShot) throws InterruptedException {
        //点击中转换乘
        webDriver.findElement(By.linkText("中转换乘")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        //定位起始点
        WebElement fromField = webDriver.findElement(By.id("fromStationText"));
        fromField.click();
        fromField.sendKeys(from + Keys.ENTER);
        //定位目的地
        WebElement toField = webDriver.findElement(By.id("toStationText"));
        toField.click();
        toField.sendKeys(to + Keys.ENTER);
        //起始日
        WebElement fromTimeField = webDriver.findElement(By.id("train_start_date"));
        fromTimeField.clear();
        fromTimeField.sendKeys(fromTime);
        webDriver.findElement(By.name("is_input_search")).click();
        //换乘站
        WebElement changeField = webDriver.findElement(By.id("changeStationText"));
        changeField.click();
        changeField.sendKeys(change + Keys.ENTER);
        //点击查询按钮
        WebElement query = webDriver.findElement(By.id("_a_search_btn"));
        query.click();
        //截图
        Thread.sleep(2500);
        takeScreenshot(screenShot);
    }

//    @AfterEach
//    public void teardown() {
//        this.webDriver.quit();
//    }

    private void takeScreenshot(String fileName) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmssSSS");
        String timestamp = dateFormat.format(new Date());
        String timestampedFileName = timestamp + "_" + fileName;
        File screenshotsDir = new File("screenshots");
        if (!screenshotsDir.exists()) {
            screenshotsDir.mkdirs();
        }
        String screenshotFilePath = screenshotsDir.getAbsolutePath() + File.separator + timestampedFileName;
        File screenshotFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenshotFile, new File(screenshotFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
